<?php
// $Id: cache.inc,v 1.19 2008/06/14 17:42:42 merlinofchaos Exp $
/**
 * @file cache.inc
 *
 * Functions to cache raid objects during editing.
 */


/**
 * Cache a raid object
 *
 * @param $node
 *   The raid node object to store
 */
function mewdkp_cache_set($raid) {
	mewdkp_cache_clear($raid->nid);
	db_query("INSERT INTO {mewdkp_object_cache} SET nid = %d, sid = '%s', updated = %d, object = '%s'", $raid->nid, session_id(), time(), serialize($raid));
}

function mewdkp_cache_get($raid_id) {
	$result = db_query("SELECT object FROM {mewdkp_object_cache} WHERE nid = %d AND sid = '%s'", $raid_id, session_id());
	$rec = db_fetch_object($result);
	$cache = $rec->object;
	if (!empty($cache)) {
		return unserialize($cache);
	}
	return NULL;
}	

function mewdkp_cache_clear($nid) {
	db_query("DELETE FROM {mewdkp_object_cache} WHERE nid = %d", $nid);
}

/**
 * Remove all objects in the object cache that are older than the
 * specified age.
 *
 * @param $age
 *   The minimum age of objects to remove, in seconds. For example, 86400 is
 *   one day. Defaults to 7 days.
 */
function mewdkp_cache_clean($age = NULL) {
  if (empty($age)) {
    $age = 86400 * 7; // 7 days
  }
  db_query("DELETE FROM {mewdkp_object_cache} WHERE updated < %d", time() - $age);
}

/**
 * @}
 */
